package com.zh.note.demo.javaSE;

import java.util.ArrayList;
import java.util.List;

public class NameList {
    private List list = new ArrayList();

    public synchronized void add(String name) {
        list.add(name);
    }

    public synchronized void printAll() {
        for (int i = 0; i < list.size(); i++) {
            System.out.println(Thread.currentThread().getName() + ":" + list.get(i) + "");
        }
    }

    public static void main(String[] args) {
        final NameList sl = new NameList();
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                sl.add("A");
                sl.add("B");
                sl.add("C");
                sl.printAll();
            }).start();
        }
    }
}